package com.ys.service.storage.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ys.config.MenuId;
import com.ys.entity.storage.TccAssemble;
import com.ys.entity.storage.TccLocation;
import com.ys.entity.storage.TccPuton;
import com.ys.entity.storage.TccStockControl;
import com.ys.entity.storage.TccStockControlRunning;
import com.ys.entity.storage.TccStockSource;
import com.ys.enume.storage.TccAssembleEnum;
import com.ys.enume.storage.TccPutonEnum;
import com.ys.enume.storage.TccStockSourceEnum;
import com.ys.mapper.storage.TccPutonMapper;
import com.ys.mapper.storage.TccStockControlMapper;
import com.ys.mapper.storage.TccStockControlRunningMapper;
import com.ys.mapper.storage.TccStockSourceMapper;
import com.ys.service.storage.TccPutonService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;

@Service("tccPutonService")
public class TccPutonServiceImpl implements TccPutonService {

	@Autowired
	private TccPutonMapper tccPutonMapper;
	@Autowired
	private TccStockControlMapper tccStockControlMapper;
	
	@Autowired
	private CommonApiService<TccPuton> commonApiService;

	@Autowired
	private CheckUtil checkUtil;

	@Autowired
	private TccStockControlRunningMapper tccStockControlRunningMapper;

	@Autowired
	private TccStockSourceMapper tccStockSourceMapper;


	@Override
	@Transactional
	public R add(String bidAssemble, Integer sourceType, Integer numParent,Long stockControlId) throws Exception {
		
		TccAssemble tccAssemble = new TccAssemble().setBid(bidAssemble).queryFirst();
		if (tccAssemble.getState() != TccAssembleEnum.State.YES.value) {
			return R.error("组装单尚未完成，无法添加");
		}

		String bid = commonApiService.getCommonNextBid(MenuId.TCC_Puton);
		String chw = null;
		Integer putonStatus = TccPutonEnum.PutonStatus.NOT.value;
		TccPuton tccPuton = TccPuton.tccPuton(bid, bidAssemble, stockControlId, chw, putonStatus, 1);
		tccPutonMapper.insert(tccPuton);

		return R.ok("操作成功");
	}


	//质检上架
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R doQualityTesting(Map<String, Object> params) throws Exception{
		if(checkUtil.isNullByObject(params.get("id"))){
			return R.error("id信息为空");
		}
		if(checkUtil.isNullByObject(params.get("chw"))){
			return R.error("货位信息为空");
		}
		TccPuton entity =new TccPuton().setId(Long.parseLong(params.get("id").toString())).queryFirst();
		if(entity!=null && entity.getBid()!=null){
			if(entity.getPutonStatus()==1){
				return R.error("已上架的单据不做处理");
			}
			entity.setChw(params.get("chw").toString());//货位编码
			tccPutonMapper.update(entity);//修改
			return R.ok();
		}else {
			return R.error("未查询到质检上架单据信息");
		}
	}

	//上架审核
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R putonExamine(Map<String, Object> params) throws Exception{
		//获取数据详情
		//Map<String, Object> info = getInfoDetail(Long.parseLong(params.get("id").toString()));
		TccPuton info = new TccPuton().setId(Long.parseLong(params.get("id").toString())).queryFirst();

		if(info!=null && info.getAssembleBid()!=null){
			if(checkUtil.isNullByObject(info.getChw())){
				return R.error("请先进行质检上架！");
			}
			if(info.getPutonStatus()==1){
				return R.error("已上架，无需再操作！");
			}
			//获取库存信息
			TccStockControl tccStockControl = new TccStockControl().setId(info.getStockControlId()).queryFirst();
			if(tccStockControl==null || tccStockControl.getProductCode()==null){
				return R.error("未查询到库存信息！");
			}

			//1、上架状态变更为已上架、填入表单“审核时间”
			TccPuton entity = new TccPuton();
			entity.setId(Long.parseLong(params.get("id").toString()));
			entity.setPutonStatus(TccPutonEnum.PutonStatus.YES.value);//上架状态(0.待上架|1已上架)
			tccPutonMapper.update(entity);

			//2、库存表信息修改
			//tccStockControl.setId(info.getStockControlId());
			//库存天数 = 上架审核时间-当天
			tccStockControl.setSdays(0);//默认0
			//入库时间 = 上架审核时间
			tccStockControl.setDrksj(entity.getUpdateTime());
			//货位编码
			tccStockControl.setChw(info.getChw());

			//通过货位编码查询信息，兼容1.0的字段
			Map<String, Object> tccLocationMap = new TccLocation()
					.addLeftJoin("TCC_WarehouseArea CWA ON CWA.Ckq = A.Ckq")//库区
					.addLeftJoin("TCC_Warehouse CW ON CW.Cck = CWA.Cck")//仓库
					.addField("CW.vck,CWA.vkq,A.vhw")
					.setChw(info.getChw()).queryFirstMap();
			String vHw = tccLocationMap.get("vhw").toString();
			String vKq = tccLocationMap.get("vkq").toString();
			String gDepot = tccLocationMap.get("vck").toString();
			tccStockControl.setVhw(vHw);
			tccStockControl.setVkq(vKq);
			tccStockControl.setGdepot(gDepot);
			tccStockControl.setProductNum(tccStockControl.getProductNumAll());

			tccStockControlMapper.update(tccStockControl);


			String  remark = "质检上架变更可用库存";
			//新增库存流水
			TccStockControlRunning tccStockControlRunning = TccStockControlRunning.tccStockControlRunning(tccStockControl.getId(), null, null,
					info.getBid(),
					tccStockControl.getVgsbm(),
					tccStockControl.getProductCode(),tccStockControl.getProductNumAll(),tccStockControl.getProductNum(),tccStockControl.getChw(),remark,1);
			tccStockControlRunningMapper.insert(tccStockControlRunning);

			//新增流水对应的数据来源等字段信息
			TccStockSource tccStockSource = TccStockSource.tccStockSource(TccStockSourceEnum.Source.TCC_Puton.value,info.getBid(),info.getId(),tccStockControlRunning.getId(),info.getAssembleBid(),0,1);
			tccStockSourceMapper.insert(tccStockSource);


		}else {
			return R.error("未查询到质检上架单信息");
		}
		return R.ok();
	}


	//根据id获取数据详情
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public Map<String,Object> getInfoDetail(Long id) throws Exception{
		TccPuton cond = setSeachSqlByCommon(new TccPuton());
		Map<String,Object> extraParam = cond
				.setId(id)
				.queryFirstMap();
		return  extraParam;
	}


	//设置查询sql
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public TccPuton setSeachSqlByCommon(TccPuton cond) throws Exception{
		cond
				.addLeftJoin("TCC_StockControl SC ON SC.Id = A.StockControlId")//库存表
				.addField("SC.RkBid rkBid")//

				.addLeftJoin("TCC_Location L ON L.Chw = A.Chw")
				.addLeftJoin("TCC_WarehouseArea CWA ON CWA.Ckq = L.Ckq")//库区
				.addLeftJoin("TCC_Warehouse CW ON CW.Cck = CWA.Cck")//仓库
				.addField("CW.vck,CWA.vkq,L.vhw")

				.addLeftJoin("TCC_Assemble AB ON AB.Bid = A.AssembleBid")//组装单
				//.addField("AB.Bid assembleBid")//商品组装单号
				.addField("AB.ParentAssembleNumber parentAssembleNumber")//母件组装数量
				.addField("AB.ParentSinglePrice parentSinglePrice")//母件采购单价

				.addLeftJoin("TCG_PurchaseAppointOrderItem PC ON PC.Id = AB.AppointOrderItemId")//预约采购管理母件表
				.addLeftJoin("TCG_PurchaseAppointOrder PPC ON PPC.Bid = PC.Pbid")//预约采购单母表
				.addLeftJoin("THR_OperateCompanyApply OCA ON OCA.Vgsbm = PPC.Vgsbm")//运营公司管理表

				.addField("PPC.Bid AS appointOrderBid")//预约采购单单号
				.addField("PPC.Vgsbm AS vgsbm")//运营公司编码
				.addField("OCA.Vgsjc AS vgsjc")//运营公司简称
				.addField("OCA.Vgsmc AS vgsmc")//运营公司名称
				.addField("PC.ProductCode AS productCode")//商品编码
				.addField("PC.ProductName AS productName")//商品名称
				.addField("PC.CodeMerge AS codeMerge")//合并商品编码
				.addField("PC.ReserveNumber AS reserveNumber")//母件预约数量、母件采购数量

				.addLeftJoin("TCG_PurchaseAppointOrder O ON O.Id = AB.AppointOrderId ")//预约采购管理表
				.addLeftJoin("TB_CompanyInfo CI ON CI.Vid = O.CompanyVid ")//公司信息
				.addField("O.CompanyVid AS companyVid")
				.addField("CI.Vname AS vname")//购买方公司名称


		;

		return cond;
	}


}
